using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BUS;
using Utility;
using DAL;

public partial class admin_CauHoi_BinhLuan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
        LoadDropDownList();
    }
    private void LoadDropDownList()
    {
        tbKhachHangCollection khachHangCollection = KHACHHANG_BUS.GetAll();
        ddl_KhachHang.DataTextField = tbKhachHangColums.HOTEN.ToString();
        ddl_KhachHang.DataValueField = tbKhachHangColums.MAKH.ToString();
        ddl_KhachHang.DataSource = khachHangCollection;
        ddl_KhachHang.DataBind();
        ddl_KhachHang.Items.Insert(0, new ListItem("All", ""));

        MatHangColection matHangCollection = MATHANG_BUS.GetAll();
        ddl_MatHang.DataTextField = MatHangColumns.TenMH.ToString();
        ddl_MatHang.DataValueField = MatHangColumns.MaMH.ToString();
        ddl_MatHang.DataSource = matHangCollection;
        ddl_MatHang.DataBind();
        ddl_MatHang.Items.Insert(0, new ListItem("All", ""));

    }

    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_ID.Text != "")
        {
            c.add(Condition.AND, tbCauHoiBinhLuanColumns.ID, Condition.LIKE, txt_ID.Text);
        }
        if (ddl_KhachHang.SelectedValue != "")
        {
            c.add(Condition.AND, tbCauHoiBinhLuanColumns.MaKH, Condition.EQUAL,
               int.Parse(ddl_KhachHang.SelectedValue));
        }
        if (ddl_MatHang.SelectedValue != "")
        {
            c.add(Condition.AND, tbCauHoiBinhLuanColumns.MaMH, Condition.EQUAL,
               int.Parse(ddl_MatHang.SelectedValue));
        }
        int total = 0;
        tbCauHoiBinhLuanCollection cauHoiCollection =
            CauHoiBinhLuan_BUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbCauHoiBinhLuanColumns.ID.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = cauHoiCollection;
        Repeater1.DataBind();

        Pager.ItemCount = total;
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
        {
            CauHoiBinhLuan_DTO cauHoiBL = (CauHoiBinhLuan_DTO)e.Item.DataItem;
            HyperLink hyp_ID = (HyperLink)e.Item.FindControl("ID");

            Literal mat_hang = (Literal)e.Item.FindControl("l_matHang");
            MatHang_DTO matHang = MATHANG_BUS.GetByID(cauHoiBL.MaMH);
            Literal khach_hang = (Literal)e.Item.FindControl("l_khachHang");
            KhachHang_DTO khachHang = KHACHHANG_BUS.GetByID(cauHoiBL.MaKH);
            mat_hang.Text = matHang.TenMH;
            khach_hang.Text = khachHang.HoTen;

            hyp_ID.Text = cauHoiBL.ID.ToString();
            hyp_ID.NavigateUrl = "~/Admin/A_ChiTiet_CauHoi_BinhLuan.aspx?id=" + cauHoiBL.ID.ToString();

            HyperLink hpltraloi = (HyperLink)e.Item.FindControl("hpl_TraLoi");
            hpltraloi.NavigateUrl = "~/admin/A_TraLoi_Them.aspx?ID=" + cauHoiBL.ID.ToString();
        }
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                CauHoiBinhLuan_DTO cauHoi = CauHoiBinhLuan_BUS.GetByID(id);

                CauHoiBinhLuan_BUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
